package com.lanou.mall.utils;

import com.github.pagehelper.PageInfo;
import com.lanou.mall.vo.MallResult;

import java.util.HashMap;

public class ResultUtils {

    private static final int DEFAULT_SUCCESS_CODE = 200;
    private static final String DEFAULT_SUCCESS_MESSAGE = "ok";
    private static final String DEFAULT_ERROR_MESSAGE = "error";

    public static MallResult ok(Object data) {
        MallResult result = ok();
        result.setData(data);
        return result;
    }

    public static MallResult ok(PageInfo page) {
        MallResult result = ok();
        result.setData(page.getList());
        result.setPaging(new HashMap<String, Object>());
        result.getPaging().put("page", page.getPageNum());
        result.getPaging().put("size", page.getPageSize());
        result.getPaging().put("total", page.getTotal());
        return result;
    }

    public static MallResult ok() {
        MallResult result = new MallResult();
        result.setCode(DEFAULT_SUCCESS_CODE);
        result.setMessage(DEFAULT_SUCCESS_MESSAGE);
        return result;
    }

    public static MallResult error(int code) {
        MallResult result = new MallResult();
        result.setCode(code);
        result.setMessage(DEFAULT_ERROR_MESSAGE);
        return result;
    }

    public static MallResult error(int code, String message) {
        MallResult result = new MallResult();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

}
